<blockquote class="layui-elem-quote">
    <div class="layui-inline">
        <i class="layui-icon layui-icon-list layui-head-icon"></i>
        <span class="layui-font-16 layui-font-green">积分设置</span>
    </div>
</blockquote>
<div class="layui-card">
    <div class="layui-card-body">
        <form class="layui-form">
            <input type="hidden" name="id" value="{$row.id|default=0}">
            <div class="layui-form-item">
                <label class="layui-form-label">积分赠送比例</label>
                <div class="layui-input-inline">
                    <input type="number" name="give_integral_scale" value="{$row.give_integral_scale|default=0}" lay-verify="required" placeholder=""
                           autocomplete="off" class="layui-input">
                </div>
                <div class="layui-form-mid layui-word-aux layui-font-red">%</div>
            </div>
            <div class="layui-form-item layui-form-text">
                <label class="layui-form-label">积分说明</label>
                <div class="layui-input-block">
                    <textarea name="integral_desc" placeholder="请输入积分说明" class="layui-textarea">{$row.integral_desc|default=''}</textarea>
                </div>
            </div>
            <hr>
            <div class="layui-form-item layui-form-text">
                <label class="layui-form-label">积分兑换金豆比列</label>
                <div class="layui-input-inline">
                    <input type="number" name="integral_gold_scale" value="{$row.integral_gold_scale|default=0}" class="layui-input">
                    <div class="layui-form-mid layui-word-aux layui-font-red">兑换比例:如一颗金豆=10个积分1:10,请填写10 </div>
                </div>
            </div>
            <div class="layui-form-item layui-form-text">
                <label class="layui-form-label">金豆说明</label>
                <div class="layui-input-block">
                    <textarea name="gold_desc" placeholder="请输入金豆说明" class="layui-textarea">{$row.gold_desc|default=''}</textarea>
                </div>
            </div>
            <hr>
            <div class="layui-form-item layui-form-text">
                <label class="layui-form-label">金豆兑换余额比列</label>
                <div class="layui-input-inline">
                    <input type="text" name="gold_balance_scale" value="{$row.gold_balance_scale|default=0}" class="layui-input">
                    <div class="layui-form-mid layui-word-aux layui-font-red">兑换比例:如一颗金豆=0.1元,请填写0.1 </div>
                </div>
            </div>
            <hr>
            <div class="layui-form-item">
                <label class="layui-form-label">是否开启用户签到</label>
                <div class="layui-input-block">
                    <input type="radio" name="is_sign_in" {notempty name='row.is_sign_in'}checked{/notempty} value="1"  title="开启">
                    <input type="radio" name="is_sign_in" {empty name='row.is_sign_in'}checked{/empty} value="0" title="关闭">
                </div>
            </div>
            <div class="layui-form-item layui-form-text">
                <label class="layui-form-label">签到奖励积分</label>
                <div class="layui-input-inline">
                    <input type="number" name="sign_in_integral" value="{$row.sign_in_integral|default=1}" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">连续签到奖励配置</label>
                <div class="layui-input-block">
                    <table class="layui-table" id="sign-table">
                        <tr>
                            <th>连续签到天数</th>
                            <th>积分奖励</th>
                            <th>优惠券奖励</th>
                            <th>操作</th>
                        </tr>
                        {notempty name='row.sign_in_config'}
                            {foreach $row.sign_in_config as $vo}
                            <tr>
                                <td>
                                    <div class="layui-inline">
                                        <input type="number" value="{$vo.sign_in_day|default=''}" class="layui-input sign_in_day">
                                    </div>
                                    <span class="layui-font-red">天</span>

                                </td>
                                <td>
                                    <div class="layui-inline">
                                        <input type="number" value="{$vo.give_integral|default=''}"  class="layui-input award give_integral">
                                    </div>
                                    <span class="layui-font-red">个</span>
                                </td>
                                <td>
                                    <a class="layui-btn layui-btn-sm choose-coupon">选择优惠券</a>
                                    <input type="hidden" class="coupon-info" value="{$vo.coupon_info|default=''}">
                                </td>
                                <td>
                                    <a class="layui-btn layui-btn-primary layui-border-red layui-btn-sm del-tr">删除</a>
                                </td>
                            </tr>
                            {/foreach}
                        {else/}
                            <tr>
                                <td>
                                    <div class="layui-inline">
                                        <input type="number" value="0" class="layui-input sign_in_day">
                                    </div>
                                    <span class="layui-font-red">天</span>

                                </td>
                                <td>
                                    <div class="layui-inline">
                                        <input type="number" value="0"  class="layui-input award give_integral">
                                    </div>
                                    <span class="layui-font-red">个</span>
                                </td>
                                <td>
                                    <a class="layui-btn layui-btn-sm choose-coupon">选择优惠券</a>
                                    <input type="hidden" class="coupon-info">
                                </td>
                                <td>
                                    <a class="layui-btn layui-btn-primary layui-border-red layui-btn-sm del-tr">删除</a>
                                </td>
                            </tr>
                        {/notempty}
                    </table>
                    <div style="text-align: center">
                        <a id="add-tr" class="layui-btn layui-btn-primary layui-border-green">添加</a>
                    </div>
                </div>
            </div>
            <hr>

            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn" lay-submit lay-filter="baseForm">保存配置</button>
                </div>
            </div>
        </form>
    </div>
</div>
<script>
    layui.use(['form'], function () {
        var form = layui.form;

        $('#add-tr').click(function (){
            let tpl = ' <tr>\n' +
                '                                <td>\n' +
                '                                    <div class="layui-inline">\n' +
                '                                        <input type="number" value="0" class="layui-input sign_in_day">\n' +
                '                                    </div>\n' +
                '                                    <span class="layui-font-red">天</span>\n' +
                '\n' +
                '                                </td>\n' +
                '                                <td>\n' +
                '                                    <div class="layui-inline">\n' +
                '                                        <input type="number" value="0"  class="layui-input award give_integral">\n' +
                '                                    </div>\n' +
                '                                    <span class="layui-font-red">个</span>\n' +
                '                                </td>\n' +
                '                                <td>\n' +
                '                                    <a class="layui-btn layui-btn-sm choose-coupon">选择优惠券</a>\n' +
                '                                    <input type="hidden" class="coupon-info">\n' +
                '                                </td>\n' +
                '                                <td>\n' +
                '                                    <a class="layui-btn layui-btn-primary layui-border-red layui-btn-sm del-tr">删除</a>\n' +
                '                                </td>\n' +
                '                            </tr>';
            $('#sign-table').append(tpl);
        })

        $('#sign-table').on('click','.del-tr',function (){
            $(this).parents('tr').remove();
        });

        function getUseCouponInfo(param) {

            $('#param').val(JSON.stringify(param));

            let coupons = [], name = '', img = '';

            param.forEach(function (item) {
                coupons.push({
                    coupon_id: item.coupon_id,
                    give_num: item.give_num
                });
                name += item.coupon_name + ';;',
                    img += '<div class="layui-inline"> ' +
                        '<img src="' + item.coupon_icon + '" class="layui-view-img">' +
                        '<i data-id="' + item.coupon_id + '" class="layui-icon layui-view-img-close">&#x1006</i>' +
                        '</div>';
            });

            $('#coupons-info').val(JSON.stringify(coupons));
            $('#imgs').html(img);
            $('#give-coupon').val(name);
        }

        $(document).on('click','.choose-coupon',function () {
            let that = $(this);
            layer.open({
                title: ['选择优惠券'],
                type: 2,
                area: ['1000px', '800px'],
                offset: '30px',
                btn: ['确定', '取消'],
                maxmin: true,
                content: '/store/market/coupon/option',
                yes: function (index, layero) {
                    let checkbox = $(layero).find('iframe').contents().find('.son:checked');
                    if (checkbox.length < 1) {
                        layer.msg('请选择优惠券!', {icon: 2});
                        return false;
                    }

                    let couponInfo = [];

                    checkbox.each(function () {
                        couponInfo.push({
                            coupon_id: $(this).val(),
                            give_num: $(this).parents('tr').find('.give_num').val()
                        });
                    })

                    that.parent('td').find('.coupon-info').val(JSON.stringify(couponInfo));


                    parent.layer.close(index);
                }
            });

        });

        function getSignRule(){

            let tr =  $('#sign-table tr:not(:eq(0))');
            if (tr.length > 0){
                let sign_rule= [];
                tr.each(function (){
                    let coupon_info = $(this).find('.coupon-info').val();
                    sign_rule.push({
                        sign_in_day : $(this).find('.sign_in_day').val(),
                        give_integral: $(this).find('.give_integral').val(),
                        coupon_info:  coupon_info ? JSON.parse(coupon_info) : ''
                    })
                })
                return sign_rule;
            }

            return  '';
        }


        form.on('submit(baseForm)', function (data) {
            let sign_rule = getSignRule();
            data.field.sign_in_config = sign_rule;
            $.post('/store/market/integral/setting', data.field, function (res) {
                if (res.code == 1) {
                    layer.msg(res.msg,{icon:1});
                    location.reload();
                    return false;
                }
                return layer.msg(res.msg, {icon: 2});
            });

            return false;
        });


    });
</script>